package 面试2025.贝壳找房;

import java.util.ArrayList;
import java.util.List;

public class Z字型变换_6 {
    public String convert(String s, int numRows) {
        if(numRows < 2) return s;
        List<StringBuilder> rows = new ArrayList<>();
        for(int i = 0; i < numRows; i++) {
            rows.add(new StringBuilder());
        }
        int k = 0;
        boolean flag = false;
        for(int i = 0;i < s.length();i++){
            rows.get(k).append(s.charAt(i));
            if(k == 0 || k == numRows -1) {
                flag = !flag;
            }
            if (flag) {
                k++;
            } else {
                k--;
            }
        }
        StringBuilder res = new StringBuilder();
        for(StringBuilder row : rows) res.append(row);
        return res.toString();
    }
}
